<!DOCTYPE html>
<html>

<head>
  <!-- USE DEVELOPMENT VERSION -->
  <script src="./konva.min.js"></script>
  <meta charset="utf-8" />
  <title>Konva boundBoxFunc 矩形图形限制缩放尺寸</title>
  <style>
    body {
      margin: 0;
      padding: 0;
      overflow: hidden;
      background-color: #f0f0f0;
    }
  </style>
</head>

<body>
  <div id="container"></div>
  <script>
    var width = window.innerWidth
    var height = window.innerHeight

    var stage = new Konva.Stage({
      container: 'container',
      width: width,
      height: height
    })

    var layer = new Konva.Layer()
    stage.add(layer)

    var rect = new Konva.Rect({
      x: 160,
      y: 60,
      width: 100,
      height: 90,
      fill: 'red',
      name: 'rect-1',
      stroke: 'black',
      draggable: true
    })
    layer.add(rect)

    var MAX_WIDTH = 200
    // create new transformer
    var tr = new Konva.Transformer({
      boundBoxFunc: function (oldBoundBox, newBoundBox) {
        /* 
            限制缩放尺寸
             dBoundBox 对象包含x、y、width和height属性
                oldBoundBox 变换前的边界框对象
                newBoundBox 变换后的边界框对象
        */
        //  限制 宽度 
        if (Math.abs(newBoundBox.width) > MAX_WIDTH) {
          return oldBoundBox
        }

        return newBoundBox
      }
    })
    layer.add(tr)
    tr.attachTo(rect)
    layer.draw();
  </script>
</body>

</html>